<?php
namespace App\Exports;

use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use App\Models\DriverWithdraw;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;

class PassengerExport implements FromCollection, WithEvents ,WithStrictNullComparison
{
    private $row;
    private $data;
    public $rowNum;



    public function __construct($row,$data)
    {

        $this->row = $row;
        $this->data = $data;
    }

    /**
     * registerEvents.
     * 事件监听
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            // 生成表单元后处理事件
            AfterSheet::class => function (AfterSheet $event) {
                // 设置单元格内容居中
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setVertical('center');
                //设置区域单元格水平居中
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setHorizontal('center');
                //设置字体大小
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getFont()->setSize(11);
                // 定义列宽度
                $widths = ['A' =>5, 'B' =>30, 'C' =>40, 'D' => 35, 'E' => 12, 'F' => 15, 'G' => 18, 'H' => 12,'I'=>18,'J'=>60,'K'=>60,'L'=>12];
                foreach ($widths as $k => $v) {
                    // 设置列宽度
                    $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
                }
                //设置行高，$i为数据行数
                for ($i = 0; $i <= $this->rowNum; $i++) {
                    $event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(25);
                }
                // 设置表中字体
                $event->sheet->getDelegate()->getStyle('A1:Z1265')->applyFromArray([
                    'font' => [
                        'name' => '宋体',
                        'bold' => false,
                        'italic' => false,
                        'strikethrough' => false,
                        'color' => [
                            'rgb' => '000000',
                        ],
                    ],
                ]);

            },
        ];

    }



    /**
     * collection.
     *
     * @return Collection
     *
     * @throws Exception
     */
    public function collection()
    {
        $row = $this->row;
        $data = $this->data;
        $parms = [];
        foreach ($data as $v){
            $parms[]=[
                'id'=>$v->id,
                'created_at'=>$v->created_at,
                'pay_sn'=>"\t" .$v->pay_sn,
                'order_sn'=>"\t" .$v->order_sn,
                'price'=>$v->price,
                'name'=>$v->name,
                'u_phone'=>"\t" .$v->u_phone,
                'real_name'=>$v->real_name,
                'phone'=>"\t" .$v->phone,
                'reality_start'=>$v->reality_start,
                'reality_end'=>$v->reality_end,
                'number'=>$v->number,
            ];
        }
        foreach ($parms as $item) {
            array_push($row, (array)$item);
        }

        return collect($row);
    }
}
